package com.ikingtech.platform.service.system.post.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.base.model.PageResult;
import com.ikingtech.framework.sdk.context.security.Me;
import com.ikingtech.framework.sdk.post.model.PostQueryParamDTO;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.system.post.entity.PostDO;
import com.ikingtech.platform.service.system.post.mapper.PostMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tie yan
 */
@Slf4j
@Service
public class PostService extends ServiceImpl<PostMapper, PostDO> {

    public List<PostDO> listByTenantCode(String tenantCode) {
        return this.list(Wrappers.<PostDO>lambdaQuery()
                .eq(PostDO::getTenantCode, tenantCode)
                .orderByDesc(PostDO::getCreateTime));
    }

    public List<PostDO> listByIdsAndTenantCode(List<String> ids, String tenantCode) {
        return this.list(Wrappers.<PostDO>lambdaQuery()
                .in(PostDO::getId, ids)
                .eq(PostDO::getTenantCode, tenantCode));
    }

    public Integer getMaxSortOrder() {
        List<Number> orders = this.listObjs(Wrappers.<PostDO>lambdaQuery()
                .select(PostDO::getSortOrder)
                .eq(PostDO::getTenantCode, Me.tenantCode())
                .orderByDesc(PostDO::getSortOrder), Number.class::cast);
        return Tools.Coll.isBlank(orders) ? 0 : orders.get(0).intValue();
    }

    public PageResult<PostDO> listPage(PostQueryParamDTO queryParam, String tenantCode) {
        return PageResult.build(this.page(new Page<>(queryParam.getPage(), queryParam.getRows()), Wrappers.<PostDO>lambdaQuery()
                .like(Tools.Str.isNotBlank(queryParam.getName()), PostDO::getName, queryParam.getName())
                .like(Tools.Str.isNotBlank(queryParam.getRemark()), PostDO::getRemark, queryParam.getRemark())
                .like(Tools.Str.isNotBlank(queryParam.getCreateName()), PostDO::getCreateName, queryParam.getCreateName())
                .eq(PostDO::getTenantCode, tenantCode)
                .orderByDesc(PostDO::getCreateTime)));
    }

    public Boolean exist(String id) {
        return this.baseMapper.exists(Wrappers.<PostDO>lambdaQuery().eq(PostDO::getId, id));
    }

    public Boolean nameExist(String name) {
        return this.baseMapper.exists(Wrappers.<PostDO>lambdaQuery()
                .isNotNull(PostDO::getId)
                .eq(PostDO::getName, name)
                .eq(PostDO::getTenantCode, Me.tenantCode()));
    }

    public Boolean nameExist(String id, String name) {
        return this.baseMapper.exists(Wrappers.<PostDO>lambdaQuery()
                .ne(PostDO::getId, id)
                .eq(PostDO::getName, name)
                .eq(PostDO::getTenantCode, Me.tenantCode()));
    }

    public <T> List<PostDO> between(T start, T end) {
        return this.list(Wrappers.<PostDO>lambdaQuery()
                .between(PostDO::getSortOrder, start, end)
                .eq(PostDO::getTenantCode, Me.tenantCode()));
    }

    public void removeByTenantCode(String tenantCode) {
        this.remove(Wrappers.<PostDO>lambdaQuery().eq(PostDO::getTenantCode, tenantCode));
    }

    public List<PostDO> listByName(String name, String tenantCode) {
        return this.list(Wrappers.<PostDO>lambdaQuery()
                .like(PostDO::getName, name)
                .eq(PostDO::getTenantCode, tenantCode));
    }
}
